package com.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.Result;
import com.entity.Label;
import com.query.LabelQuery;
import com.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wty
 * @since 2021-03-16
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 给任务设置标签
     */
    @RequestMapping("/setLabelForTask")
    public Result setLabelForTask(@RequestBody  LabelQuery labelQuery){
       return  labelService.setLabelForTask(labelQuery);
    }

    /**
     * 添加标签
     */
    @RequestMapping("/addLabel")
    public Result addLabel(@RequestBody Label label){
        int max=0;
        //查询优先级最大的
        Wrapper<Label> wr=new EntityWrapper<>();
        wr.orderBy("priority",false);
        List<Label> labels = labelService.selectList(wr);
        if(!CollectionUtils.isEmpty(labels)){
            max=labels.get(0).getPriority();
        }
        max++;
        label.setPriority(max);
        label.setExecuteTime("[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]");
        boolean insert = labelService.insert(label);
        if(insert){
            return  Result.success("ture");
        }else{
            return  Result.error("false");
        }
    }

    /**
     * 获取全部标签
     * or根据条件获取标签
     */
    @RequestMapping("/getLabelList")
    public  Result getLabelList(){
        Wrapper<Label> wr =new EntityWrapper<>();
        wr.orderBy("priority",true);
        List<Label> labels =  labelService.selectList(wr);
        return  Result.success(labels);
    }

    /**
     * 编辑标签优先级
     */
    @RequestMapping("/editLabel")
    public  Result editLable(@RequestBody List<Label> labels){
        boolean flag = labelService.updateBatchById(labels);
        if(flag){
            return  Result.success("更新成功");
        }else{
            return  Result.error("更新失败");
        }
    }

    /**
     * 根据id获取标签信息
     */
    @RequestMapping("/getLabel/{id}")
    public Result getLabel(@PathVariable Integer id){
        Wrapper<Label> wr=new EntityWrapper<>();
        wr.eq("id",id);
        Label label = labelService.selectOne(wr);

        return  Result.success(label);
    }

    /**
     * 根据id删除标签
     */
    @RequestMapping("/delLabel/{id}")
    public  Result delLabel(@PathVariable Integer id){
        boolean b = labelService.deleteById(id);
        if(b){
            return  Result.success(b);
        }else{
            return  Result.error("删除失败");
        }
    }

    /**
     * 更新执行时间段
     */
    @RequestMapping("/updateLabelById")
    public Result updateLabelById(@RequestBody Label label){
        boolean b = labelService.updateById(label);
        if(b){
            return  Result.success("更新成功");
        }else{
            return  Result.error("更新失败");
        }
    }


}

